#include "../include/head.h"

void conf_get(char* path, Conf* conf) {
    FILE *fp = fopen(path, "r");
    if (fp == NULL) {
        perror("open conf file");
        exit(EXIT_FAILURE);
    }

    char line[128];
    char key[64], value[64];

    while (fgets(line, sizeof(line), fp)) {
        // 跳过注释行和空行
        if (line[0] == '#' || line[0] == '\n')
            continue;

        // 去掉末尾换行符
        line[strcspn(line, "\n")] = 0;

        // 按 key=value 解析
        if (sscanf(line, "%63[^=]=%63s", key, value) == 2) {
            if (strcmp(key, "host") == 0) {
                memcpy(conf->host,value,strlen(value));
            } else if (strcmp(key, "port") == 0) {
                 memcpy(conf->port,value,strlen(value));
            } else if (strcmp(key, "worker_num") == 0) {
                conf->worker_num = atoi(value);
            }
        }
    }

    fclose(fp);
}
